package org.iabako.client.navigation;

import com.google.gwt.user.client.Window;
import org.iabako.client.config.page.PageEnum;
import org.iabako.client.config.page.PageNavigationType;
import org.iabako.client.ui.form.AbstractForm;
import org.iabako.client.ui.layout.template.RootPage;
import org.iabako.client.ui.layout.widget.dialogbox.IabakoDialogBox;
import org.iabako.client.ui.page.AbstractSearchPage;
import org.iabako.shared.exception.IabakoException;
import org.iabako.shared.tools.GenericTools;

/**
 * TODO javadoc
 * Date: 24/11/13
 * Time: 17:39
 */
public class NavigationController {

    //In GWT, each tab in each browser the module runs under has its own version of static attribute
    private static NavigationController instance;

    //The pageContent at a specific moment
    private RootPage rootPage;

    public static NavigationController getInstance() {
        if (instance == null) {
            instance = new NavigationController();
        }
        return instance;
    }

    public NavigationController() {
        if (GenericTools.isEmpty(Window.Location.getHash())) {
            setHomeURL();
        }
    }

    public RootPage getRootPage() {
        return rootPage;
    }

    public void setRootPage(RootPage rootPage) {
        this.rootPage = rootPage;
    }

    public void show(IabakoDialogBox iabakoDialogBox) {
        rootPage.setPopupLayout(true);
        iabakoDialogBox.show();
    }

    public void goTo(PageEnum page, String... urlParams) {
        goTo(page, null, urlParams);
    }

    public void goTo(AbstractForm abstractForm) {
        if (abstractForm.isSimplePopup()) {
            this.rootPage.setPopupSimpleContent(abstractForm);
        } else {
            this.rootPage.setPopupPageContent(abstractForm);
        }
    }

    public void goTo(AbstractSearchPage abstractSearchPage) {
        this.rootPage.setPageContent(abstractSearchPage);
    }

    public void goTo(PageEnum page, AbstractForm parentForm, String... urlParams) {

        if (page.getPageNavigationType() != PageNavigationType.popupSimple) {
            this.rootPage.setPopupPageContent(null);
        }

        formatURL(page, urlParams);

        try {
            if (page.getPageNavigationType() == PageNavigationType.popup) {
                this.rootPage.setPopupPageContent(PageInstanceGetter.getPageInstance(page, parentForm));
            } else if (page.getPageNavigationType() == PageNavigationType.popupSimple) {
                this.rootPage.setPopupSimpleContent(PageInstanceGetter.getPageInstance(page, parentForm));
            } else {
                this.rootPage.setPageContent(PageInstanceGetter.getPageInstance(page, parentForm));
            }
        } catch (IabakoException e) {
            IabakoDialogBox iabakoDialogBox = new IabakoDialogBox(e.getUiMessageTitle(), e.getUiMessageText(), e.getIconStyle(), true, "action_forbidden_by_package_ok", false);
            iabakoDialogBox.setAvoidClosePopupCall(!e.isClosePopups());
            show(iabakoDialogBox);

        }
    }

    private void formatURL(PageEnum page, String... urlParams) {
        String newURL = "";
        if (page.getPageNavigationType() == PageNavigationType.popup
                || page.getPageNavigationType() == PageNavigationType.popupSimple) {
            newURL = Window.Location.getHref() + "#" + page.name();
        } else {
            String oldHash = Window.Location.getHash();
            newURL = Window.Location.getHref().replace(oldHash, "") + "#" + page.name();
        }
        if (urlParams != null) {
            for (String param : urlParams) {
                newURL += "@" + param;
            }
        }
        Window.Location.replace(newURL);
    }

    public void setHomeURL() {
        formatURL(PageEnum.Home);
    }

    public void setIabakoHomeURL() {
        formatURL(PageEnum.IabakoHome);
    }

    public void closePopups() {
        closePopup(true);
        closePopup(false);
    }

    public void closePopup(final boolean simplePopup) {
        if (simplePopup) {
            rootPage.setPopupSimpleContent(null);
        } else {
            rootPage.setPopupPageContent(null);
        }
        removeLastHash();
    }

    private void removeLastHash() {
        if (Window.Location.getHref().split("#").length > 2) {
            String lastHash = Window.Location.getHref().substring(Window.Location.getHref().lastIndexOf("#"));
            String newURL = Window.Location.getHref().replace(lastHash, "");
            Window.Location.replace(newURL);
        }
    }

    public String[] getParams() {
        String hash = Window.Location.getHash();
        if (GenericTools.isEmpty(hash) || !hash.contains("#")) {
            return null;
        }
        String lastHash = hash.substring(hash.lastIndexOf("#"), hash.length());
        if (!lastHash.contains("@")) {
            return null;
        }
        String params = lastHash.substring(lastHash.indexOf("@"));
        if (params.startsWith("@")) {
            return params.substring(1).split("@");
        } else {
            return params.split("@");
        }
    }
}
